//
//  AssetsList.m
//  OntheRoadV4
//
//  Created by ZhangYanhua on 15/1/15.
//  Copyright (c) 2015年 EricHuang. All rights reserved.
//

#import "AssetsList.h"
#import "ALAssetsGroupEnumerator.h"
#import "QWImageMangerConfig.h"

@implementation AssetsList

- (id)initWithAssetsGroup:(MyAssetsGroup *)assetsGroup delegate:(id)delegate
{
    self = [super init];
    if (self) {
        _myGroup = assetsGroup;
        _delegate = delegate;
    }
    return self;
}

- (id)initWithAssetsGroup:(MyAssetsGroup *)assetsGroup
        photosFetchResult:(PHFetchResult *)photosFetchResult
        videosFetchResult:(PHFetchResult *)videosFetchResult
{
    self = [super init];
    if (self) {
        _myGroup = assetsGroup;
        
        _photosFetchResult = photosFetchResult;
        _videosFetchResult = videosFetchResult;
    }
    return self;
}

- (void)dealloc
{
    _delegate = nil;
    _stopQuery = YES;
}

- (NSArray *)photos
{
    if (!_photos) {
        _photos = [NSMutableArray arrayWithCapacity:_photosFetchResult.count];
    }

    return _photos;
}

- (NSArray *)videos
{
    if (!_videos) {
        _videos = [NSMutableArray arrayWithCapacity:_videosFetchResult.count];
    }
    
    return _videos;
}

#pragma mark -
#pragma mark Assets Loading

- (void)loadAssetsWithEnumerationBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock {
    if (self.myGroup.assetGroup) {
        [self loadAssetsFromAssetsGroup:self.myGroup.assetGroup
                     callerResultsBlock:enumerationBlock];

    } else {
        NSAssert(0, @"group can't be nil");
    }
}

- (void)loadAssetsForPHAssetsCollection
{
    if (_photosFetchResult) {
        for (int i = 0; i < _photosFetchResult.count; i++) {
            ImageAsset *imgAsset = [[ImageAsset alloc] initWithPHFetchResult:_photosFetchResult
                                                                     isPhoto:YES
                                                                  assetIndex:i];
            imgAsset.groupIdentifier = self.myGroup.groupIdentifier;

            [self.photos addObject:imgAsset];
        }
    }

    if (_videosFetchResult) {
        for (int i = 0; i < _videosFetchResult.count; i++) {
            ImageAsset *imgAsset = [[ImageAsset alloc] initWithPHFetchResult:_videosFetchResult
                                                                     isPhoto:NO
                                                                  assetIndex:i];
            imgAsset.groupIdentifier = self.myGroup.groupIdentifier;
            
            [self.videos addObject:imgAsset];
        }
    }
}

- (void)loadAssetsFromAssetsGroup:(ALAssetsGroup *)group
               callerResultsBlock:(ALAssetsGroupEnumerationResultsBlock)callerResultsBlock {
    
    ALAssetsGroupEnumerationResultsBlock callerResultsBlockCopy = callerResultsBlock;
    
    [QWImageMangerConfig sharedAssetsLibrary];
    
    ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
        if (asset == nil) {
            if (self.enumStatus != ASSETS_ENUM_ABORT) {
                self.enumStatus = ASSETS_ENUM_FINISHED;
            }
            if ([self.delegate respondsToSelector:@selector(asstesListenumFinished:)]) {
                [self.delegate asstesListenumFinished:self];
            }
        }

        if (_stopQuery) {
            self.enumStatus = ASSETS_ENUM_ABORT;
            *stop = YES;
        }
        else if (asset) {
            NSString *assetType = [asset valueForProperty:ALAssetPropertyType];

            ImageAsset *imgAsset = [[ImageAsset alloc] initWithALAsset:asset];
            imgAsset.groupIdentifier = self.myGroup.groupIdentifier;

            if ([assetType isEqualToString:ALAssetTypePhoto]) {
                [self.photos addObject:imgAsset];
            } else if ([assetType isEqualToString:ALAssetTypeVideo]) {
                [self.videos addObject:imgAsset];
            }
        }

        if (callerResultsBlockCopy) {
            callerResultsBlockCopy(asset, [self.photos count] + [self.videos count], &_stopQuery);
        }
    };

    self.enumStatus = ASSETS_ENUM_IN_PROGRESS;
    [self.videos removeAllObjects];
    [self.photos removeAllObjects];
    [group setAssetsFilter:[ALAssetsFilter allAssets]];
    [group enumerateAssetsWithOptions:NSEnumerationReverse
                           usingBlock:resultsBlock];
}


@end
